package com.biye.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.common.edu.entity.Department;
import com.base.common.edu.entity.EduClass;
import com.base.common.edu.entity.Staff;
import com.base.common.edu.mapper.ClassMapper;
import com.base.common.edu.mapper.DepartmentMapper;
import com.base.common.edu.mapper.StaffMapper;
import com.base.common.edu.util.Page;
import com.base.common.httpStatusCode.enumDefine.StatusCode;
import com.biye.api.service.ClassManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ClassManageServiceImpl implements ClassManageService {

    @Resource
    private ClassMapper classMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private StaffMapper staffMapper;

    // 班级新增
    @Override
    public JSONObject saveClass(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "新增操作无效，请稍后再试";
        int statusCode = StatusCode.Failure._statusCode;
        // 检查参数的合法性
        if ( !requestMap.containsKey("gradeYear") || !requestMap.containsKey("className") || !requestMap.containsKey("classNo") || !requestMap.containsKey("deptId") || !requestMap.containsKey("staffId") ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 获取请求参数, 并对参数进行空值校验
        String gradeYear = (String) requestMap.get("gradeYear");
        gradeYear = "".equals(gradeYear) ? null : gradeYear;
        String className = (String) requestMap.get("className");
        className = "".equals(className) ? null : className;
        String classNo = (String) requestMap.get("classNo");
        String deptId = (String) requestMap.get("deptId");
        String staffId = (String) requestMap.get("staffId");
        // 这三个参数不可为空
        if ( "".equals(gradeYear) || "".equals(className) || "".equals(classNo) || "".equals(deptId) ) {
            json.put("msg", "请求参数不可为空");
            json.put("statusCode", statusCode);
            return json;
        }
        // 这个classNo参数不可为空
        Integer classNo_ = null;
        try{
            classNo_ = (!"".equals(classNo)) ? Integer.parseInt(classNo) : null;
        }catch (Exception e){
            // 在这里这可能是转化错误，该字符串不是纯数字或一个有效数字
            json.put("msg", "Class_No字段转化出错，请填写有效纯数字");
            json.put("statusCode", statusCode);
            return json;
        }
        // 由于以下两个字段为外键，若不为空，则需要判断外键的参照键是否存在
        deptId = "".equals(deptId) ? null : deptId;
        if ( deptId != null ){
            Department department = departmentMapper.queryDepartmentById(deptId);
            if ( department == null ) deptId = null;
        }
        staffId = "".equals(staffId) ? null : staffId;
        if ( staffId != null ){
            Staff staff = staffMapper.queryStaffById(staffId);
            if ( staff == null ) staffId = null;
        }
        // 下边开始封装参数
        EduClass eduClass = new EduClass();
        eduClass.setClassName(className);
        eduClass.setClassNo(classNo_);
        eduClass.setGradeYear(gradeYear);
        if ( deptId != null ){
            Department dept = new Department();
            dept.setDeptId(deptId);
            eduClass.setDeptObject(dept);
        }
        if ( staffId != null ){
            Staff staff = new Staff();
            staff.setStaffId(staffId);
            eduClass.setStaff(staff);
        }
        // 进行保存
        int i = classMapper.addClass(eduClass);
        if ( i > 0 ){
            msg = "新增班级信息成功";
            statusCode = StatusCode.Success._statusCode;
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    // 班级修改
    @Override
    public JSONObject amendClass(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "班级信息修改失败，请稍后再试";
        int statusCode = StatusCode.Failure._statusCode;
        // 检查参数的合法性
        if ( !requestMap.containsKey("classId") || !requestMap.containsKey("gradeYear") || !requestMap.containsKey("className") || !requestMap.containsKey("classNo") || !requestMap.containsKey("deptId") || !requestMap.containsKey("staffId") ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 原有参数会进行填充，新修改的参数也会改变
        String classId = (String) requestMap.get("classId");
        if ( "".equals(classId) ){
            json.put("msg", "A critical error occured, pls check and try again later!");
            json.put("statusCode", statusCode);
            return json;
        }
        String gradeYear = (String) requestMap.get("gradeYear");
        gradeYear = "".equals(gradeYear) ? null : gradeYear;
        String className = (String) requestMap.get("className");
        className = "".equals(className) ? null : className;
        int classNo = (int) requestMap.get("classNo");
        String deptId = (String) requestMap.get("deptId");
        String staffId = (String) requestMap.get("staffId");
        staffId = "".equals(staffId)? null : staffId;
        /***
         * 填充参数
         */
        // 由于以下两个字段为外键，若不为空，则需要判断外键的参照键是否存在
        deptId = "".equals(deptId) ? null : deptId;
        if ( deptId != null ){
            Department department = departmentMapper.queryDepartmentById(deptId);
            if ( department == null ) deptId = null;
        }
        if ( staffId != null ){
            Staff staff_isExist = staffMapper.queryStaffById(staffId);
            staffId = null;
            if ( staff_isExist != null )
                staffId = staff_isExist.getStaffId();
        }
        // 下边开始封装参数
        EduClass eduClass = new EduClass();
        eduClass.setClassId(classId);
        eduClass.setClassName(className);
        eduClass.setClassNo(classNo);
        eduClass.setGradeYear(gradeYear);
        if ( deptId != null ){
            Department dept = new Department();
            dept.setDeptId(deptId);
            eduClass.setDeptObject(dept);
        }
        if ( staffId != null ){
            Staff staff = new Staff();
            staff.setStaffId(staffId);
            eduClass.setStaff(staff);
        }
        // 修改数据
        int i = classMapper.updateClass(eduClass);
        if ( i > 0 ){
            msg = "修改班级信息成功";
            statusCode = StatusCode.Success._statusCode;
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    // 班级删除
    @Override
    public JSONObject delClass(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "删除操作无效，请稍后再试";
        int statusCode = StatusCode.Failure._statusCode;
        // 检查参数的合法性
        if ( !requestMap.containsKey("classId") ) {
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 获取参数，校验参数是否非空
        String classId = (String) requestMap.get("classId");
        if ( "".equals(classId) ) {
            json.put("msg", "请求参数不可为空");
            json.put("statusCode", statusCode);
            return json;
        }
        // 执行删除操作
        int i = classMapper.delClass(classId);
        if ( i > 0 ){
            msg = "该班级信息已擦除";
            statusCode = StatusCode.Success._statusCode;
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    // 班级查询：下拉框
    @Override
    public JSONObject queryClassForDropDownList() {
        JSONObject json = new JSONObject();
        String msg = "暂无数据";
        int statusCode = StatusCode.Failure._statusCode;
        // 查询数据
        List<EduClass> eduClasses = classMapper.queryClass(new EduClass());
        if ( eduClasses.size() > 0 ) {
            msg = "获取数据成功";
            statusCode = StatusCode.Success._statusCode;
            List<Map<String, Object>> sa_list = new ArrayList<>();
            Map<String, Object> temp_map = null;
            for ( EduClass ec : eduClasses ){
                temp_map = new HashMap<>();
                temp_map.put("classId", ec.getClassId());
                temp_map.put("className", ec.getClassName());
                sa_list.add(temp_map);
            }
            json.put("list", sa_list);
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    // 班级查询
    @Override
    public JSONObject queryClassList(Map<String, Object> requestMap) {
        JSONObject json = new JSONObject();
        String msg = "暂无数据";
        int statusCode = StatusCode.Failure._statusCode;
        // 请求参数合法性校验
        if (    !requestMap.containsKey("pageNum") || !requestMap.containsKey("pageSize")
                || !requestMap.containsKey("classId") || !requestMap.containsKey("gradeYear") || !requestMap.containsKey("deptId") || !requestMap.containsKey("staffId") ){
            json.put("msg", "请求参数不正确");
            json.put("statusCode", statusCode);
            return json;
        }
        // 获取请求参数
        int pageNum = (int) requestMap.get("pageNum");
        int pageSize = (int) requestMap.get("pageSize");
        String classId = (String) requestMap.get("classId");
        classId = "".equals(classId) ? null : classId ;
        String gradeYear = (String) requestMap.get("gradeYear");
        gradeYear = "".equals(gradeYear) ? null : gradeYear ;
        String deptId = (String) requestMap.get("deptId");
        deptId = "".equals(deptId) ? null : deptId ;
        String staffId = (String) requestMap.get("staffId");
        staffId = "".equals(staffId) ? null : staffId ;
        // 拼接参数
        EduClass eduClass = new EduClass();
        eduClass.setClassId(classId);
        eduClass.setGradeYear(gradeYear);
        if ( deptId != null ){
            Department dept = new Department();
            dept.setDeptId(deptId);
            eduClass.setDeptObject(dept);
        }
        if ( staffId != null ){
            Staff staff = new Staff();
            staff.setStaffId(staffId);
            eduClass.setStaff(staff);
        }
        /***
         * 分页查询数据
         */
        PageHelper.startPage(pageNum, pageSize);
        List<EduClass> class_list = classMapper.queryClass(eduClass);
        if ( class_list.size() > 0 ) {
            PageInfo<EduClass> pageInfo = new PageInfo<>(class_list);
            Page<Map<String, Object>> p = new Page<Map<String, Object>>(pageNum, pageSize, (int) pageInfo.getTotal());
            List<Map<String, Object>> sa_list = new ArrayList<>();
            Map<String, Object> map_obj;
            for ( EduClass cla : class_list ) {
                map_obj = new HashMap<>();
                map_obj.put("classId", cla.getClassId());
                map_obj.put("className", cla.getClassName());
                map_obj.put("classNo", cla.getClassNo());
                map_obj.put("gradeYear", cla.getGradeYear());
                if ( cla.getDeptObject() != null ){
                    map_obj.put("deptId", cla.getDeptObject().getDeptId());
                    map_obj.put("deptName", cla.getDeptObject().getDeptName());
                }else{
                    map_obj.put("deptId", "");
                    map_obj.put("deptName", "");
                }
                if ( cla.getStaff() != null ){
                    map_obj.put("staffId", cla.getStaff().getStaffId());
                    map_obj.put("staffName", cla.getStaff().getName());
                }else{
                    map_obj.put("staffId", "");
                    map_obj.put("staffName", "");
                }
                sa_list.add(map_obj);
            }
            p.setList(sa_list);
            msg = "数据查询成功";
            statusCode = StatusCode.Success._statusCode;
            json.put("page", p);
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

}
